package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcNsxzService;
import com.augurit.gzcgwsp.jzfqwczz.web.form.YwYnJzfqwczcJbxxForm;
import com.augurit.gzcgwsp.readAndParseXML.service.ICzzNsxzService;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 处置证 年审续证
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class CzzNsxzServiceImpl implements ICzzNsxzService{
	Logger logger = (Logger) LoggerFactory.getLogger(CzzNsxzServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;
	@Autowired
	private IYwYnJzfqwczcNsxzService ywYnJzfqwczcNsxzService;
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String czzNsxzfilepath = localResource.getString("czzNsxzfilepath");

	// 附件存放路径
	private final String czzNsxzfujianpath = localResource.getString("czzNsxzfujianpath");

	// 解析成功反馈xml存放路径
	private final String czzNsxzfeedbackPath = localResource.getString("czzNsxzfeedbackPath");

	// 解析异常xml存放路径
	private final String czzNsxzexceptionPath = localResource.getString("czzNsxzexceptionPath");

	// 解析后，xml备份
	private final String czzNsxzbackPath = localResource.getString("czzNsxzbackPath");

	private Map<String, String> czzNsxzKeyMap;// xml内容的key

	private Map<String, String> czzNsxzKeyMapHead;// xml头部的key

	private Map<String, Long> czzNsxzfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initczzNsxzMap() {
		// 初始化数据部分对应的map
		czzNsxzKeyMap = new HashMap<String, String>();
		czzNsxzKeyMap.put("approvalNo", "approvalNo");
		czzNsxzKeyMap.put("businessCode", "businessCode");
		czzNsxzKeyMap.put("YSL_NO", "YSL_NO");
		czzNsxzKeyMap.put("sendTime", "sendTime");
		
		
		czzNsxzKeyMap.put("SQDW", "SQDW");
		czzNsxzKeyMap.put("QYJC", "QYJC");
		czzNsxzKeyMap.put("QYPHONE", "QYPHONE");
		czzNsxzKeyMap.put("ADDRESS", "ADDRESS");
		czzNsxzKeyMap.put("POST", "POST");
		czzNsxzKeyMap.put("CZ", "CZ");
		czzNsxzKeyMap.put("FRXM", "FRXM");
		czzNsxzKeyMap.put("FRPHONE", "FRPHONE");
		czzNsxzKeyMap.put("YWLXR", "YWLXR");
		czzNsxzKeyMap.put("YWLXRTEL", "YWLXRTEL");
		czzNsxzKeyMap.put("ZYDM", "ZYDM");
		czzNsxzKeyMap.put("GSZCDJH", "GSZCDJH");
		czzNsxzKeyMap.put("GSZCDJYXQ", "GSZCDJYXQ");
		czzNsxzKeyMap.put("QYDLYSZH", "QYDLYSZH");
		czzNsxzKeyMap.put("QYDLYSZYXQ", "QYDLYSZYXQ");
		czzNsxzKeyMap.put("ZZDJ", "ZZDJ");
		czzNsxzKeyMap.put("CLS", "CLS");
		czzNsxzKeyMap.put("SFYX", "SFYX");
		czzNsxzKeyMap.put("CZZH", "CZZH");
		czzNsxzKeyMap.put("BRANCH", "BRANCH");
		

		// 初始化头部对应的map
		czzNsxzKeyMapHead = new HashMap<String, String>();
		czzNsxzKeyMapHead.put("sn", "sn");
		czzNsxzKeyMapHead.put("sender", "sender");
		// czzNsxzKeyMapHead.put("receiver", "receiver");
		czzNsxzKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		czzNsxzfujianMap = new HashMap<String, Long>();
//		czzNsxzfujianMap.put("FJ_01", 204l);
//		czzNsxzfujianMap.put("FJ_02", 205l);
//		czzNsxzfujianMap.put("FJ_03", 206l);
//		czzNsxzfujianMap.put("FJ_04", 207l);
//		czzNsxzfujianMap.put("FJ_05", 208l);
//		czzNsxzfujianMap.put("FJ_06", 209l);
	}

	public void readXml(String projectPath) {
		String templeCode = "";
		if (czzNsxzKeyMap == null)
			this.initczzNsxzMap();
		File file = new File(czzNsxzfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(czzNsxzKeyMap, filePath,czzNsxzfeedbackPath, czzNsxzexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(czzNsxzKeyMapHead,files[i], czzNsxzfeedbackPath, czzNsxzexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczcJbxxForm form = new YwYnJzfqwczcJbxxForm();
						for (String mapKey : czzNsxzKeyMap.keySet()) {
							
							form.setYslNo(parseXMLService.getMapValue(czzNsxzKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(czzNsxzKeyMap.get("SQDW")));
							templeCode = parseXMLService.getMapValue(czzNsxzKeyMap.get("BRANCH"));
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(czzNsxzKeyMap.get("SQDW")),templeCode);
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}
							
							form.setQyjc(parseXMLService.getMapValue(czzNsxzKeyMap.get("QYJC")));
							form.setQyphone(parseXMLService.getMapValue(czzNsxzKeyMap.get("QYPHONE")));
							form.setAddress(parseXMLService.getMapValue(czzNsxzKeyMap.get("ADDRESS")));
							form.setPost(parseXMLService.getMapValue(czzNsxzKeyMap.get("POST")));
							form.setCz(parseXMLService.getMapValue(czzNsxzKeyMap.get("CZ")));
							form.setFrxm(parseXMLService.getMapValue(czzNsxzKeyMap.get("FRXM")));
							form.setFrphone(parseXMLService.getMapValue(czzNsxzKeyMap.get("FRPHONE")));
							form.setYwlxr(parseXMLService.getMapValue(czzNsxzKeyMap.get("YWLXR")));
							form.setYwlxrtel(parseXMLService.getMapValue(czzNsxzKeyMap.get("YWLXRTEL")));
							form.setZydm(parseXMLService.getMapValue(czzNsxzKeyMap.get("ZYDM")));
							form.setGszcdjh(parseXMLService.getMapValue(czzNsxzKeyMap.get("GSZCDJH")));
							String gszcdjyxqStr = parseXMLService.getMapValue(czzNsxzKeyMap.get("GSZCDJYXQ"));
							Date gszcdjyxq = null;
							if (StringUtils.isNotEmpty(gszcdjyxqStr)) {
								gszcdjyxq = DateUtils.stringToDate(gszcdjyxqStr, DateUtils.YYYY_MM_DD);
								form.setGszcdjyxq(gszcdjyxq);
							}
							
							form.setQydlyszh(parseXMLService.getMapValue(czzNsxzKeyMap.get("QYDLYSZH")));
							String qydlyszyxqStr = parseXMLService.getMapValue(czzNsxzKeyMap.get("QYDLYSZYXQ"));
							Date qydlyszyxq = null;
							if (StringUtils.isNotEmpty(qydlyszyxqStr)) {
								qydlyszyxq = DateUtils.stringToDate(qydlyszyxqStr, DateUtils.YYYY_MM_DD);
								form.setQydlyszyxq(qydlyszyxq);
							}
							form.setZzdj(parseXMLService.getMapValue(czzNsxzKeyMap.get("ZZDJ")));
							form.setCls(parseXMLService.getMapValue(czzNsxzKeyMap.get("CLS")));
							form.setSfyx(parseXMLService.getMapValue(czzNsxzKeyMap.get("SFYX")));
							form.setCzzh(parseXMLService.getMapValue(czzNsxzKeyMap.get("CZZH")));							
							
							form.setBranch(templeCode);
							form.setApprovalNo(parseXMLService.getMapValue(czzNsxzKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(czzNsxzKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(czzNsxzKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(
										wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywYnJzfqwczcNsxzService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									czzNsxzKeyMapHead, czzNsxzfeedbackPath, czzNsxzexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, czzNsxzfujianpath, czzNsxzfeedbackPath, czzNsxzexceptionPath, czzNsxzKeyMapHead,
								files, czzNsxzfujianMap, i, form.getQyid(), form.getId(), templeCode);
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								czzNsxzKeyMapHead, czzNsxzfeedbackPath, czzNsxzexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, czzNsxzbackPath + fileName,fileName);
				}
			}
		}
	}

}
